﻿using Microsoft.Practices.Prism.Mvvm;
using System;
using System.ComponentModel.Composition;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace Songhay.BiggestBox.Desktop.Modules.Exceptions.Views
{
    using Portable.Shared.Models;

    /// <summary>
    /// Interaction logic for ExceptionsView.xaml
    /// </summary>
    [Export("ExceptionsView", typeof(IView))]
    public partial class ExceptionsView : UserControl, IView
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="ExceptionsView"/> class.
        /// </summary>
        public ExceptionsView()
        {
            InitializeComponent();
            ViewModelLocator.SetAutoWireViewModel(this, true);

            this.ShouldCloseApp.Checked += (s, args) =>
            {
                var app = Application.Current as IApp;

                if(this.ShouldCloseApp.IsChecked.GetValueOrDefault())
                {
                    app.ShouldCloseOnDispatcherException = true;
                }
                else
                {
                    app.ShouldCloseOnDispatcherException = false;
                }
            };
        }

        void ThrowException(object sender, RoutedEventArgs e)
        {
            throw new SystemException("BiggestBox: this is an Exception thrown on the UI thread.");
        }

        async void ThrowThreadException(object sender, RoutedEventArgs e)
        {
            await Task.Factory.StartNew(() =>
            {
                throw new SystemException("BiggestBox: this is an Exception thrown on a Task thread.");
            });
        }
    }
}
